package com.wb.utils

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.awt.Color
import java.io.File
import java.io.InputStream
import java.io.ObjectInput
import java.nio.charset.Charset
import java.time.LocalDate
import java.time.format.DateTimeFormatter

/**
 * 原神工具
 * @author 祁雪
 */
class YuanShenUtil {

    companion object {

        fun getImg(path: String, temPath: String, charset: Charset): InputStream {

            val fileName = "${LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))}.png"

            var file = File(path, fileName)
            if (file.exists()) {
                return file.inputStream()
            }

            val json = ResourceUtil.readFileByRes("yuanshen.json", charset)

            val gson = Gson()
            val map = gson.fromJson(
                json,
                object : TypeToken<HashMap<String, Any>>() {}.type
            ) as HashMap<String, Any>

            // 随机三个属性
            val data = getAttr(map)

            // 生成图片
            val width = 939
            val height = 1084
            val contentList = arrayListOf<ImageBusinessUtil.ImageContent>(
                // 日期
                ImageBusinessUtil.ImageContent().apply {
                    text = LocalDate.now().year.toString()
//            color = Color.lightGray
                    isBold = true
                    fontSize = 40
                    x = 120
                    y = 300
                    color = Color(141, 118, 80)
                },
                ImageBusinessUtil.ImageContent().apply {
                    val day = LocalDate.now().dayOfMonth
                    text = if (day < 10) "0${day}" else day.toString()
//            color = Color.lightGray
                    fontSize = 60
                    isBold = true
                    x = 430
                    y = 305
                    color = Color(252, 253, 250)
                },
                ImageBusinessUtil.ImageContent().apply {
                    val mon = LocalDate.now().monthValue
                    text = if (mon < 10) "0$mon" else mon.toString()
//            color = Color.lightGray
                    fontSize = 40
                    isBold = true
                    x = 740
                    y = 300
                    color = Color(141, 118, 80)
                }
                // 总体数据
            )
            var topY = 400
            data.forEach { t, u ->
                u.forEach {
                    topY += 60
                    contentList.add(
                        ImageBusinessUtil.ImageContent().apply {
                            text = it.name!!
                            fontSize = 50
                            isBold = true
                            x = 200
                            y = topY
                            color = Color(141, 118, 80)
                        }
                    )
                    topY += 40
                    contentList.add(
                        ImageBusinessUtil.ImageContent().apply {
                            text =
                                if ("宜".equals(t)) it.good!!.get((Math.random() * it.good!!.size).toInt())
                                else it.bad!!.get((Math.random() * it.bad!!.size).toInt())
                            fontSize = 30
                            isBold = true
                            x = 200
                            y = topY
                            color = Color(212, 212, 212)
                        }
                    )
                }
                topY += 30
            }

            ImageBusinessUtil.outPutImage(
                ImageBusinessUtil.setContent(
                    ImageBusinessUtil.createImage(
                        width,
                        height,
                        File(temPath),
                        true
                    ),
                    contentList
                ),
                File(path, fileName)
            )

            file = File(path, fileName)

            return file.inputStream()
        }

        fun getAttr(map: HashMap<String, Any>): HashMap<String, ArrayList<DataParam>> {

            val mapData = hashMapOf<String, DataParam>()

            val resultMap = hashMapOf<String, ArrayList<DataParam>>()

            val gson = Gson()

            // 0.宜 1.忌
            val params = mapOf<String, Array<String>>(
                "宜" to arrayOf("relic", "boss", "other"),
                "忌" to arrayOf("relic", "boss", "other")
            )
            params.forEach { k, v ->
                val list = arrayListOf<DataParam>()
                v.forEach {
                    val key = if ("relic".equals(it)) "刷" else if ("boss".equals(it)) "打" else ""
                    while (true) {
                        val msg = gson.fromJson(
                            gson.toJson(map.get("${it}Msg")),
                            object : TypeToken<DataParam>() {}.type
                        ) as DataParam

                        val datas = gson.fromJson(
                            gson.toJson(map.get(it)),
                            object : TypeToken<ArrayList<DataParam>>() {}.type
                        ) as ArrayList<DataParam>
                        val data = datas.get((Math.random() * datas.size).toInt())
                        data.name = key + data.name
                        if (!mapData.containsKey(data.name)) {
                            mapData.put(data.name!!, data)
                            msg.good?.let { it1 -> data.good?.addAll(it1) }
                            msg.bad?.let { it1 -> data.bad?.addAll(it1) }
                            list.add(data)
                            break
                        }
                    }
                }
                resultMap.put(k, list)
            }

            return resultMap
        }

    }

    class DataParam {

        var name: String? = ""

        var good: MutableList<String>? = arrayListOf()

        var bad: MutableList<String>? = arrayListOf()
        override fun toString(): String {
            return "DataParam(name=$name, good=${good?.joinToString(";")}, bad=${bad?.joinToString(";")})"
        }
    }

}

fun main() {
//    YuanShenUtil.getImg("")

    val json = ResourceUtil.readFileByRes("yuanshen.json")

    val gson = Gson()
    val map = gson.fromJson(
        json,
        object : TypeToken<HashMap<String, Any>>() {}.type
    ) as HashMap<String, Any>

    // 随机属性
    val data = YuanShenUtil.getAttr(map)

    // 生成图片
    val width = 939
    val height = 1084
    val contentList = arrayListOf<ImageBusinessUtil.ImageContent>(
        // 日期
        ImageBusinessUtil.ImageContent().apply {
            text = LocalDate.now().year.toString()
//            color = Color.lightGray
            isBold = true
            fontSize = 40
            x = 120
            y = 300
            color = Color(141, 118, 80)
        },
        ImageBusinessUtil.ImageContent().apply {
            text = LocalDate.now().dayOfMonth.toString()
//            color = Color.lightGray
            fontSize = 60
            isBold = true
            x = 430
            y = 305
            color = Color(252, 253, 250)
        },
        ImageBusinessUtil.ImageContent().apply {
            val mon = LocalDate.now().monthValue
            text = if (mon < 10) "0$mon" else mon.toString()
//            color = Color.lightGray
            fontSize = 40
            isBold = true
            x = 740
            y = 300
            color = Color(141, 118, 80)
        }
        // 总体数据
    )
    var topY = 400
    data.forEach { t, u ->
        u.forEach {
            topY += 60
            contentList.add(
                ImageBusinessUtil.ImageContent().apply {
                    text = it.name!!
                    fontSize = 50
                    isBold = true
                    x = 200
                    y = topY
                    color = Color(141, 118, 80)
                }
            )
            topY += 40
            contentList.add(
                ImageBusinessUtil.ImageContent().apply {
                    text =
                        if ("宜".equals(t)) it.good!!.get((Math.random() * it.good!!.size).toInt())
                        else it.bad!!.get((Math.random() * it.bad!!.size).toInt())
                    fontSize = 30
                    isBold = true
                    x = 200
                    y = topY
                    color = Color(212, 212, 212)
                }
            )
        }
        topY += 30
    }

    ImageBusinessUtil.outPutImage(
        ImageBusinessUtil.setContent(
            ImageBusinessUtil.createImage(
                width,
                height,
                File("C:\\Users\\76167\\Desktop\\文档数据\\test\\mhy\\tem.png"),
                true
            ),
            contentList
        ),
        File("C:\\Users\\76167\\Desktop\\文档数据\\test\\mhy\\test.png")
    )

}